﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace DeploymentMaxLib.Actions
{
    public class ActionEmail : ActionBase
    {
        /// <summary>
        /// 
        /// </summary>
        public override string LongName
        {
            get { return Common.Constants.LongName; }
        }

        public override string Description
        {
            get { return Common.Constants.Description; }
        }

        public override ActionResponse Execute(ActionRequest pActionRequest)
        {
            base.Result = Validate(pActionRequest);

            if (!base.Result.HasErrorOrCritical())
            {
                var tExecutionType = pActionRequest.ExecutionType.ToEnum<Common.ExecutionType>();

                switch (tExecutionType)
                {
                    case Common.ExecutionType.SendEmail:
                        {
                            SendEmail(DataObjectSendEmail.FromXml(pActionRequest.DataObjectXml));
                        } break;
                }
            }

            return pActionRequest.GetResponse(this);
        }

        public void SendEmail(DataObjectSendEmail pSendEmail)
        {
            try
            {
                var tMail = new MailMessage();

                tMail.From = new MailAddress(pSendEmail.FromAddress, pSendEmail.FromName);

                if (pSendEmail.ToAddress.IndexOf(";") != -1)
                {
                    string[] tToList = pSendEmail.ToAddress.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string tTo in tToList)
                    {
                        tMail.To.Add(tTo);
                    }
                }
                else
                {
                    tMail.To.Add(pSendEmail.ToAddress);
                }

                tMail.Subject = pSendEmail.Subject;

                tMail.Body = pSendEmail.BodyText;

                var tSmtpClient = new System.Net.Mail.SmtpClient(pSendEmail.SmtpClientAddress, pSendEmail.SmtpClientPort);

                tSmtpClient.UseDefaultCredentials = false;

                tSmtpClient.Credentials = new System.Net.NetworkCredential(pSendEmail.SmtpUserName, pSendEmail.SmtpPassword);

                tSmtpClient.Send(tMail);

            }
            catch (Exception exc)
            {
                base.Result.AddResult(exc);
            }

        }

        public override ActionResult Validate(ActionRequest pRequest)
        {
            var tResult = new ActionResult();

            return tResult;
        }

        public class DataObjectSendEmail : DataObjectBase
        {
            public string FromAddress { get; set; }
            public string FromName { get; set; }
            public string ToAddress { get; set; }
            public string Subject { get; set; }
            public string BodyText { get; set; }
            public string SmtpClientAddress { get; set; }
            public int SmtpClientPort { get; set; }
            public string SmtpUserName { get; set; }
            public string SmtpPassword { get; set; }

            public static DataObjectSendEmail FromXml(string pXml)
            {
                return (DataObjectSendEmail)Serializer.FromXml(pXml, typeof(DataObjectSendEmail));
            }
        }

        public static class Common
        {
            public enum ExecutionType
            {
                None = 0,
                SendEmail = 1
            }

            public static class Constants
            {
                public const string LongName = "";
                public const string Description = "";

            }
        }
    }
}
